﻿using System.Diagnostics;
using System.Net;
using GalaSoft.MvvmLight.Messaging;
using httpExplorer.Message;

namespace httpExplorer.Model
{
    public class NetworkingModel : INetworkingModel
    {
        public void InitiateHttpRequest(string url, IWebProxy proxy = null)
        {
            Debug.Assert(!string.IsNullOrEmpty(url));

            // Construct the web request using GET http verb
            var webReq = (HttpWebRequest)WebRequest.Create(url);
            webReq.Method = "GET";
            webReq.AllowAutoRedirect = false;   // Want to see each redirect explicitly in the view
            InitiateHttpRequest(webReq, proxy);
        }

        public void InitiateHttpRequest(HttpWebRequest webReq, IWebProxy proxy = null)
        {
            bool isRedirect;
            do
            {
                // Set the proxy
                webReq.Proxy = proxy;
             
                // Send the request, get the response
                var webResp = (HttpWebResponse)webReq.GetResponse();

                // Output the response to the view model
                Messenger.Default.Send(new ResponseMessage { Request = webReq, Response = webResp });

                int statusCode = (int)webResp.StatusCode;

                // Follow redirects, if instructed to do so
                isRedirect = statusCode >= 300 && statusCode < 400;
                if (isRedirect)
                {
                    string url = webResp.Headers["Location"];
                    isRedirect = !string.IsNullOrEmpty(url);
                }
            } while (isRedirect);
        }

        public void InitiateHttpRequestUsingSystemProxy(string url)
        {
            Debug.Assert(!string.IsNullOrEmpty(url));

            // Construct the web request using GET http verb
            var webReq = (HttpWebRequest)WebRequest.Create(url);
            webReq.Method = "GET";
            webReq.AllowAutoRedirect = false;   // Want to see each redirect explicitly in the view
            webReq.UseDefaultCredentials = true;// Provide proxy with the login credentials
            var proxy = WebRequest.GetSystemWebProxy();
            proxy.Credentials = CredentialCache.DefaultCredentials;
            InitiateHttpRequest(webReq, proxy);
        }

        public void InitiateHttpRequestUsingProxy(string url, string proxyUri, bool useDefaultCredentials = false)
        {
            Debug.Assert(!string.IsNullOrEmpty(proxyUri));
            var proxy = new WebProxy(proxyUri);
            if ( useDefaultCredentials )
                proxy.Credentials = CredentialCache.DefaultCredentials;
            InitiateHttpRequest(url, proxy);
        }

        public void InitiateHttpRequestUsingProxy(string url, string proxyUri, string username, string password, string domain)
        {
            Debug.Assert(!string.IsNullOrEmpty(proxyUri));
            Debug.Assert(!string.IsNullOrEmpty(username));
            Debug.Assert(!string.IsNullOrEmpty(password));
            Debug.Assert(!string.IsNullOrEmpty(domain));

            // Create a NetworkCredential object and is assign to the Credentials property of the Proxy object.
            var proxy = new WebProxy(proxyUri) {Credentials = new NetworkCredential(username, password, domain)};
            InitiateHttpRequest(url, proxy);
        }
    }
}
